#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Config.GlobalVariables import TESTSUITE_PATH
from Config.TestCasesList import TEST_CASES_LIST
from Utils.Logger import Logger
import unittest


class TestSet(object):

    def __init__(self):
        self.logger = Logger().get_logger()

    def all_cases(self):
        try:
            test_set = unittest.defaultTestLoader.discover(TESTSUITE_PATH, pattern='*.py')
            return test_set
        except Exception as e:
            self.logger.error("Make Test Suite Error: {0}".format(e)[:-1])
            raise e

    def listed_cases(self):
        try:
            test_set = unittest.TestSuite()
            test_set.addTests(TEST_CASES_LIST)
            return test_set
        except Exception as e:
            self.logger.error("Make Test Suite Error: {0}".format(e)[:-1])
            raise e

    def specific_case(self, test_class_name, test_case_name):
        try:
            test_set = unittest.TestSuite()
            for test_case in TEST_CASES_LIST:
                class_name = str(test_case.__class__).replace("<class '", "").replace("'>", "").split(".")[-1]
                case_name = str(test_case._testMethodName)
                if class_name == test_class_name and case_name == test_case_name:
                    test_set.addTest(test_case)
                    return test_set
            raise NameError
        except NameError:
            self.logger.error("NameError: Wrong Class name or Case name")
            raise
        except Exception as e:
            self.logger.error("Make Test Suite Error: {0}".format(e)[:-1])
            raise e

if __name__ == '__main__':
    testSet = TestSet()
    testSet.specific_case("TestLogin", "test01_LoginSuccessfully")
